//
//  HMBillingRecordController.m
//  HMSupplier
//
//  Created by 孙鹏飞 on 2017/7/6.
//  Copyright © 2017年 haohao. All rights reserved.
//

#import "HMBillingRecordController.h"
#import "BillCell.h"
#import "HMBillDetailViewController.h"
#import "HMSearchViewController.h"
#import "BileModel.h"

@interface HMBillingRecordController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableview;

@property (nonatomic, strong) NSMutableArray * monthOrderArr;

@end

@implementation HMBillingRecordController
static NSString * const ID = @"BillCell";

- (NSMutableArray *)monthOrderArr
{
    if (_monthOrderArr == nil) {
        _monthOrderArr = [NSMutableArray array];
    }
    return _monthOrderArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setupTableView];
    self.title = @"账单记录";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"筛选" style:UIBarButtonItemStylePlain target:self action:@selector(select)];

    [self setupRefresh];
//    [self setupData];
}

- (void)select
{
    HMSearchViewController *vc = [[HMSearchViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    NSLog(@"筛选");
}

#pragma mark - 初始化刷新控件
- (void)setupRefresh
{
    // header
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableview.mj_header.automaticallyChangeAlpha = YES;
    [self.tableview.mj_header beginRefreshing];
    
    // footer
    self.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

static int currentPage = 0;

/** 下拉刷新 */
- (void)loadNewData
{
    currentPage = 0;
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"uid"] = [MGAccountTool getUid];
    params[@"limit"] = @10;
    params[@"page"] = @(currentPage);
    
    [HttpBaseTool post:kTMonthRecordUrl params:params success:^(id responseObj) {
        
        self.monthOrderArr = [BileModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"]];
        
        [self.tableview reloadData];
        self.tableview.mj_footer.hidden = NO;
        [self.tableview.mj_header endRefreshing];
        
    } failure:^(NSError *error) {
        [self.tableview.mj_header endRefreshing];
    }];
}

/** 上拉加载 */
- (void)loadMoreData
{
    NSLog(@"%d",currentPage);
    currentPage++;
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"uid"] = [MGAccountTool getUid];
    params[@"limit"] = @10;
    params[@"page"] = @(currentPage);
    
    [HttpBaseTool post:kTMonthRecordUrl params:params success:^(id responseObj) {
        NSArray * moreDataArray = [BileModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"]];
        [self.monthOrderArr addObjectsFromArray:moreDataArray];
        
        [self.tableview reloadData];
        if (moreDataArray.count == 0) {
            self.tableview.mj_footer.hidden = YES;
        } else {
            [self.tableview.mj_footer endRefreshing];
        }
    } failure:^(NSError *error) {
        currentPage--;
        [self.tableview.mj_footer endRefreshing];
    }];
}

- (void)setupData
{
    NSDictionary * param = @{
                             @"uid":[MGAccountTool getUid],
                             @"page":@1,
                             @"limit":@10,
                             };
    [HttpBaseTool post:kTMonthRecordUrl params:param success:^(id responseObj) {
        NSLog(@"我的账单");
        
        self.monthOrderArr = [BileModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"]];
        
        [self.tableview reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

- (void)setupTableView
{
    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    self.tableview = tableView;
    [self.view addSubview:tableView];
    
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.backgroundColor = [UIColor whiteColor];
    self.tableview.showsVerticalScrollIndicator = NO;
    [self.tableview registerNib:[UINib nibWithNibName:@"BillCell" bundle:nil] forCellReuseIdentifier:ID];
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = CGRectMake(0, 0, 40, 40);
    [searchBtn setTitle:@"筛选" forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(pushToSearchView) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
}

- (void)pushToSearchView {
    HMSearchViewController *vc = [[HMSearchViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.monthOrderArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BillCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.model = self.monthOrderArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView * headView = [[UIView alloc] init];
//    headView.backgroundColor = [UIColor colorWithHexString:@"e5e5e5"];
//    
//    UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, self.view.width, 36)];
//    lable.text = @"本月";
//    lable.textAlignment = NSTextAlignmentLeft;
//    lable.font = [UIFont systemFontOfSize:15];
//    lable.backgroundColor = [UIColor colorWithHexString:@"e5e5e5"];
//    [headView addSubview:lable];
//    
//    return headView;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HMBillDetailViewController *vc = [[HMBillDetailViewController alloc] init];
    vc.model = self.monthOrderArr[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
